﻿using System;
using System.IO;
using System.Windows.Forms;

namespace Cleaner {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            DirectoryInfo rootDir = new DirectoryInfo("./");
            CheckDir(rootDir);
        }

        static void CheckDir(DirectoryInfo rootDirInfo) {
            // 遍历文件夹
            foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
                if (
                    dirInfo.Name == "bin" && dirInfo.Parent.Name != "assets"
                    || dirInfo.Name == "debug"
                    || dirInfo.Name == "release"
                    || dirInfo.Name == "Debug"
                    || dirInfo.Name == "Release"
                    || dirInfo.Name == ".vs"
                    || dirInfo.Name == "gen"
                    || dirInfo.Name == "obj"
                    || dirInfo.Name == "ipch"
                    || dirInfo.Name == "build"
                    || dirInfo.Name == "project.xcworkspace"
                    || dirInfo.Name == "xcuserdata"
                    || dirInfo.Name == "x64"
                    || dirInfo.Name == "out"
                    || dirInfo.Name.StartsWith("build-")
                ) {
                    dirInfo.Delete(true);
                }
                else {
                    CheckDir(dirInfo);
                }
            }

            // 遍历文件
            foreach (FileInfo fileInfo in rootDirInfo.GetFiles()) {
                CheckFile(fileInfo);
            }
        }

        static void CheckFile(FileInfo fileInfo) {
            if (
                fileInfo.Name == "vssver2.scc"
                || fileInfo.Extension == ".sdf"
                || fileInfo.Extension == ".suo"
                || fileInfo.Extension == ".aps"
                || fileInfo.Extension == ".db"
                || fileInfo.Extension == ".user"
                || fileInfo.Extension == ".filters"
                || fileInfo.Extension == ".DS_Store"
                || fileInfo.Name == "local.properties"
                || (fileInfo.Extension == ".sln"
                    && fileInfo.Name != "ImClient.sln"
                    && fileInfo.Name != "ImServer.sln"
                    && fileInfo.Name != "GomokuClient.sln"
                    && fileInfo.Name != "GomokuServer.sln")
            ) {
                fileInfo.Delete();
            }
        }
    }
}
